package com.example.tccdemo.service;

import com.example.tccdemo.db131.dao.UserMapper;
import com.example.tccdemo.db131.model.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @describte:TODO
 * @program:distribute-transaction
 * @author:zhengdp
 * @create:2021-09-27 16:51:49
 **/
@Service
@Slf4j
public class UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 返回所有用户
     * @return
     */
    public List<User> getAllUsers(){
        return userMapper.selectAllUsers();
    }

    public int delUser(Integer userId){
        User user = userMapper.selectByPrimaryKey(userId);
        if(user!= null){
            log.info("用户存在，用户Id为："+userId);
            return userMapper.deleteByPrimaryKey(userId);
        }
        log.info("用户不存在，用户Id为"+userId);
        return 0;
    }

    public User selectById(Integer userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    public int updateUser(User user) {
        return userMapper.updateUser(user);
    }

    @Autowired
    private CuratorFramework zkClient;

    public int insertUser(User user,String token) throws Exception {
        InterProcessMutex lock = new InterProcessMutex(zkClient,"/"+token);
        boolean isLock = lock.acquire(30, TimeUnit.SECONDS);
        if(isLock){
            return userMapper.insertSelective(user);
        }
        return 0;
    }
}


